package com.xihua.sx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.xihua.sx.pojo.PageData;
import com.xihua.sx.pojo.Reserve;
import com.xihua.sx.mapper.ReserveMapper;
import com.xihua.sx.pojo.ReserveSelectLike;
import com.xihua.sx.pojo.ReserveVo;
import com.xihua.sx.service.IReserveService;
import com.xihua.sx.utils.ResponseMessage;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.Wrapper;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2023-06-16
 */
@Service
public class ReserveServiceImpl  implements IReserveService {

    @Resource
    private ReserveMapper reserveMapper;


    /**
     * 分页查询
     * @param pageData
     * @return
     */
    @Override
    public ResponseMessage reserveList(PageData pageData) {

        if (pageData.getPage()==null ||pageData.getSize()==null){
            pageData.setPage(1);
            pageData.setSize(5);
        }
        //通过前端传过来的pageData对象中的数据，创建分页对象page
        Page<ReserveVo> page = new Page<>(pageData.getPage(), pageData.getSize());

        //执行查询
        Page<ReserveVo> reservePage = reserveMapper.reserveList(page);

        return new ResponseMessage(200,"reserve list",reservePage);
    }

    @Override
    public ResponseMessage reserveOne(Integer id) {
        QueryWrapper<Reserve> questionQueryWrapper = new QueryWrapper<>();
        questionQueryWrapper.eq("reserve_id",id);
        Reserve reserve = reserveMapper.selectOne(questionQueryWrapper);

        return new ResponseMessage(200,"reserveOne",reserve);

    }

    /**
     * 新增
     * @param reserve
     * @return
     */
    @Override
    public ResponseMessage insertReserve(Reserve reserve) {

        reserve.setReserveState("已提交");
        int insert = reserveMapper.insert(reserve);

        return new ResponseMessage(200,"新增预约成功！",insert);
    }

    /**
     * 删除
     * @param id
     * @return
     */
    @Override
    public ResponseMessage deleteReserve(Integer id) {
        int i = reserveMapper.deleteById(id);

        return new ResponseMessage(200,"deleteById",i);
    }

    /**
     * 更新
     * @param reserve
     * @return
     */
    @Override
    public ResponseMessage updateReserve(Reserve reserve) {
        int i = reserveMapper.updateById(reserve);

        return new ResponseMessage(200,"updateById",i);
    }

    /**
     * 模糊查询
     * @param reserveSelectLike
     * @return
     */
    @Override
    public ResponseMessage selectLike(ReserveSelectLike reserveSelectLike) {
        QueryWrapper<ReserveVo> queryWrapper = new QueryWrapper<>();
        queryWrapper.like(reserveSelectLike.getReserveTopic()!=null,"reserve_topic",reserveSelectLike.getReserveTopic());
        queryWrapper.like(reserveSelectLike.getReserveNotes()!=null,"reserve_notes",reserveSelectLike.getReserveNotes());
        List<ReserveVo> reserveVos = reserveMapper.reserveLikeList(queryWrapper);

        return new ResponseMessage(200,"selectLike",reserveVos);
    }

}
